/**
 * 数据处理
 * @author echo.
 * @version 1.5.0
 **/
const utils = {
	//格式化音频时间
	formarAudioTime: function(duration) {
		let audiodurations = duration
		let minute = Math.floor(audiodurations / 60)
		let second = Math.floor((audiodurations / 60 - minute) * 60)
		minute = minute < 10 ? `0${minute}` : (minute + '')
		second = second < 10 ? `0${second}` : (second + '')
		return minute + ':' + second
	},


	// 计算开始时间到结束时间,中间时分秒
	countStartAndEndTimes: function(createTimeNum, updateTimeNum) {
		let startTime = createTimeNum
		let endTime = updateTimeNum
		let runTime = parseInt((endTime - startTime) / 1000);
		let year = Math.floor(runTime / 86400 / 365);
		runTime = runTime % (86400 * 365);
		let month = Math.floor(runTime / 86400 / 30);
		runTime = runTime % (86400 * 30);
		let day = Math.floor(runTime / 86400);
		runTime = runTime % 86400;
		let hour = Math.floor(runTime / 3600);
		runTime = runTime % 3600;
		let minute = Math.floor(runTime / 60);
		runTime = runTime % 60;
		let second = runTime;
		//   console.log(`相差${year}年${month}月${day}天${hour}小时${minute}分${second}秒`);
		hour = hour < 10 ? '0' + hour : hour
		minute = minute < 10 ? '0' + minute : minute
		second = second < 10 ? '0' + second : second

		return `${hour}:${minute}:${second}`
	},

	// 判断是否为空
	isNullOrEmpty: function(value) {
		//是否为空
		return (value === null || value === '' || value === undefined) ? true : false;
	},

	// 数组对象去重 需要标识 arr1:数组1 arr2:数组2 fieldName:字段名称 fieldValue:z字段值
	arrObjRemoval: function(arr1, arr2, fieldName = 'vModel', fieldValue, ) {
		// 先去重
		let newArr = arr1.filter((item) => !arr2.some((ele) => ele.name === item.name));

		// 添加新字段
		// newArr.forEach((item,index) => {
		//     this.$set(item, fieldName, fieldValue);
		// });

		newArr.forEach(item => {
			Object.assign(item, {
				vModel: fieldValue
			})
		})

		return newArr // 返回新数组
	},

	// 给对象属性赋值
	forObject: function(obj, val) {
		Object.keys(obj).forEach(key => obj[key] = val);
	},

	//去空格
	trim: function(value) {
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
	//内容替换
	replaceAll: function(text, repstr, newstr) {
		return text.replace(new RegExp(repstr, "gm"), newstr);
	},
	//格式化手机号码
	formatNumber: function(num) {
		return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num;
	},
	//金额格式化
	rmoney: function(money) {
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,')
			.replace(
				/\,$/, '').split('').reverse().join('');
	},
	//日期格式化
	formatDate: function(formatStr, fdate) {
		if (fdate) {
			if (~fdate.indexOf('.')) {
				fdate = fdate.substring(0, fdate.indexOf('.'));
			}
			fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/');
			var fTime, fStr = 'ymdhis';
			if (!formatStr)
				formatStr = "y-m-d h:i:s";
			if (fdate)
				fTime = new Date(fdate);
			else
				fTime = new Date();
			var month = fTime.getMonth() + 1;
			var day = fTime.getDate();
			var hours = fTime.getHours();
			var minu = fTime.getMinutes();
			var second = fTime.getSeconds();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			hours = hours < 10 ? ('0' + hours) : hours;
			minu = minu < 10 ? '0' + minu : minu;
			second = second < 10 ? '0' + second : second;
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]
			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
			}
			return formatStr;
		} else {
			return "";
		}
	},
	rgbToHex: function(r, g, b) {
		return "#" + utils.toHex(r) + utils.toHex(g) + utils.toHex(b)
	},
	toHex: function(n) {
		n = parseInt(n, 10);
		if (isNaN(n)) return "00";
		n = Math.max(0, Math.min(n, 255));
		return "0123456789ABCDEF".charAt((n - n % 16) / 16) +
			"0123456789ABCDEF".charAt(n % 16);
	},
	hexToRgb(hex) {
		let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
		return result ? {
			r: parseInt(result[1], 16),
			g: parseInt(result[2], 16),
			b: parseInt(result[3], 16)
		} : null;
	},

	previewImage(index, list) {
		let lists
		if (typeof(list) == 'string') {
			lists = list ? list.split(",") : [];
		} else {
			lists = list
		}
		let photoList = lists.map(item => {
			return item;
		});
		uni.previewImage({
			current: index,
			urls: photoList
		});
	},
	setClipboardData(val) {
		uni.setClipboardData({
			data: val,
			success: function() {
				uni.showToast({
					title: '已复制',
					duration: 2000,
					icon:'none'
				});

			}
		});

	}
}

export default {
	formarAudioTime: utils.formarAudioTime,
	countStartAndEndTimes: utils.countStartAndEndTimes,
	isNullOrEmpty: utils.isNullOrEmpty,
	forObject: utils.forObject,
	arrObjRemoval: utils.arrObjRemoval,
	trim: utils.trim,
	replaceAll: utils.replaceAll,
	formatNumber: utils.formatNumber,
	rmoney: utils.rmoney,
	formatDate: utils.formatDate,
	rgbToHex: utils.rgbToHex,
	hexToRgb: utils.hexToRgb,
	dictKey: utils.dictKey,
	previewImage: utils.previewImage,
	setClipboardData:utils.setClipboardData
}